package com.yanqu.road.server.cmd.sync;


import com.google.protobuf.AbstractMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.ICrossServiceCommand;
import com.yanqu.road.server.connect.CrossAgentLinkedControl;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_SERVICE_FORWARD_MESSAGE, desc = "转发游戏服发给游戏服的消息")
public class ForwardMessageCmd extends CrossCommand {

    private SyncMessage.Request request;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        this.request = SyncMessage.Request.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        if (this.request.hasForwardServerId()) {
            if (this.request.getForwardServerId() > 0) {
                if (!CrossAgentLinkedControl.isSeverIdConnection(request.getForwardServerId())){
                    SyncMessage.Response.Builder builder = SyncMessage.Response.newBuilder();
                    builder.setErrorCode(GameErrorCode.E_SEARCH_SERVER_MAINTAIN);
                    builder.setSequenceId(request.getSequenceId());
                    builder.setForwardServerId(this.request.getRawServerId());
                    builder.setActivityId(this.request.getActivityId());
                    builder.setRawServerId(this.request.getForwardServerId());

                    YanQuMessage yanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_SERVICE_MESSAGE_RESP, builder);
                    MessageHelper.sendPacket(this.request.getRawServerId(), 0, yanQuMessage);
                }else{
                    packet.setCode(GameProtocol.S_GAME_SERVICE_HANDLER);
                    MessageHelper.sendPacket(this.request.getForwardServerId(), 0, packet);
                }
            }
        }
    }

    @Override
    public void sendData() {

    }
}
